package com.ruoyi.vote.controller;

import java.util.List;

import com.ruoyi.vote.domain.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vote.domain.ResultOption;
import com.ruoyi.vote.service.IResultOptionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 结果选项Controller
 *
 * @author ruoyi
 * @date 2022-10-05
 */
@Controller
@RequestMapping("/vote/resultOptions")
public class ResultOptionController extends BaseController {
    private String prefix = "vote/resultOptions";

    @Autowired
    private IResultOptionService resultOptionService;

    @RequiresPermissions("vote:resultOptions:view")
    @GetMapping()
    public String resultOptions() {
        return prefix + "/resultOptions";
    }

    /**
     * 查询结果选项列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ResultOption resultOption) {
        startPage();
        List<ResultOption> list = resultOptionService.selectResultOptionList(resultOption);
        return getDataTable(list);
    }

    /**
     * 新增结果选项
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存结果选项
     */
    @Log(title = "结果选项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ResultOption resultOption) {
        return toAjax(resultOptionService.insertResultOption(resultOption));
    }

    @PostMapping("/add1")
    @ResponseBody
    public AjaxResult save(@RequestBody ResultOption resultOption) {
        int i = resultOptionService.insertResultOption(resultOption);
        if (i > 0) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改结果选项
     */
    @GetMapping("/edit/{resultId}")
    public String edit(@PathVariable("resultId") Long resultId, ModelMap mmap) {
        ResultOption resultOption = resultOptionService.selectResultOptionByResultId(resultId);
        mmap.put("resultOption", resultOption);
        return prefix + "/edit";
    }

    /**
     * 修改保存结果选项
     */
    @Log(title = "结果选项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ResultOption resultOption) {
        return toAjax(resultOptionService.updateResultOption(resultOption));
    }

    /**
     * 删除结果选项
     */
    @Log(title = "结果选项", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(resultOptionService.deleteResultOptionByResultIds(ids));
    }
}
